﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace PongGame
{
    public class MenuManager
    {
        int currentMenuItem = 0;
        Game game;

        bool remUpPressed = false,
            remDownPressed = false,
            remSpaceOrStartPressed = false,
            remEscOrBackPressed = false;

        bool gamePadUp = false,
           gamePadDown = false;

        GamePadState gamePadState;
        KeyboardState keyboardState;
        SoundBank soundBank;

        public int CurrentMenuItemNum
        {
            get { return currentMenuItem; }
        }

        public MenuManager(Game game, SoundBank sound)
        {
            this.game = game;
            soundBank = sound;
        }

        public void Update()
        { 
            keyboardState = Keyboard.GetState();

            SelectMenuItem();
            
            RecordKeyLastState();

        }

        void RecordKeyLastState()
        {
            remUpPressed =
                gamePadState.DPad.Up == ButtonState.Pressed ||
                gamePadState.ThumbSticks.Left.Y > 0.5f ||
                keyboardState.IsKeyDown(Keys.Up);

            remDownPressed =
                gamePadState.DPad.Down == ButtonState.Pressed ||
                gamePadState.ThumbSticks.Left.Y < -0.5f ||
                keyboardState.IsKeyDown(Keys.Down);
            
            remSpaceOrStartPressed =
                gamePadState.Buttons.Start == ButtonState.Pressed ||
                gamePadState.Buttons.A == ButtonState.Pressed ||
                keyboardState.IsKeyDown(Keys.LeftControl) ||
                keyboardState.IsKeyDown(Keys.RightControl) ||
                keyboardState.IsKeyDown(Keys.Space) ||
                keyboardState.IsKeyDown(Keys.Enter);

            remEscOrBackPressed =
                gamePadState.Buttons.Back == ButtonState.Pressed ||
                keyboardState.IsKeyDown(Keys.Escape);
        }

        void SelectMenuItem()
        {
            if ((keyboardState.IsKeyDown(Keys.Down) || gamePadDown) && remDownPressed == false)
            {
                currentMenuItem = (currentMenuItem + 1) % 3;
                soundBank.PlayCue("PongBallHit");
            }

            else if ((keyboardState.IsKeyDown(Keys.Up) || gamePadUp) && remUpPressed == false)
            {
                currentMenuItem = (currentMenuItem + 2) % 3;
                soundBank.PlayCue("PongBallHit");
            }

            else if (IsGameMangementButtonsPressed() && remSpaceOrStartPressed == false && remEscOrBackPressed == false)
            {
                // Quit app.
                if (currentMenuItem == 2 ||
                    keyboardState.IsKeyDown(Keys.Escape) ||
                    gamePadState.Buttons.Back == ButtonState.Pressed)
                {
                    game.Exit();
                }
            }
        }

        private bool IsGameMangementButtonsPressed()
        {
            return (keyboardState.IsKeyDown(Keys.Space) ||
                                keyboardState.IsKeyDown(Keys.LeftControl) ||
                                keyboardState.IsKeyDown(Keys.RightControl) ||
                                keyboardState.IsKeyDown(Keys.Enter) ||
                                gamePadState.Buttons.A == ButtonState.Pressed ||
                                gamePadState.Buttons.Start == ButtonState.Pressed ||
                // Back or Escape exits our game on Xbox 360 and Windows
                                keyboardState.IsKeyDown(Keys.Escape) ||
                                gamePadState.Buttons.Back == ButtonState.Pressed);
        }
    }
}
